#include "AppUtilPrecompiled.hxx"
#include "AppUtilTimer.hxx"

using namespace HerzeleidTimingNamespaceImpl;

Timer::Timer() : TimerView(),
	m_UpdateCallback(nullptr)
{
	QueryPerformanceFrequency( _Outptr_opt_ _To_large_int_ptr &m_TicksPerSecond64 );
	QueryPerformanceCounter( _Outptr_opt_ _To_large_int_ptr &m_CurrentTicks64 );
	m_OneSecondTicks64 = m_CurrentTicks64;
	m_StartupTicks64 = m_CurrentTicks64;

	m_UpdateCallback = &Timer::UpdateNonfixedStepImpl;
}

void Timer::SetFixedTimeStep( Float32Const fStep )
{
	if ( fStep > 0.0F )
	{
		m_bUseFixedDelta = false; m_fFixedDelta = 0.0f;
		m_UpdateCallback = &Timer::UpdateNonfixedStepImpl;
	}
	else
	{
		m_bUseFixedDelta = true; m_fFixedDelta = fStep;
		m_UpdateCallback = &Timer::UpdateFixedStepImpl;
	}
}

//void Timer::Update()
//{
//	m_LastTicks64 = m_CurrentTicks64;
//
//	QueryPerformanceCounter( _Outptr_opt_ _To_large_int_ptr &m_CurrentTicks64 );
//
//	(this->*m_UpdateCallback)();
//
//	auto const High = _To_single (m_CurrentTicks64 - m_OneSecondTicks64);
//	auto const Low = _To_single m_TicksPerSecond64;
//	auto const Value = (High / Low);
//
//	if ( Value < 1.0f ) m_iFrameCount++;
//	else
//	{
//		m_OneSecondTicks64 = m_CurrentTicks64;
//		m_iFramesPerSecond = m_iFrameCount;
//		m_iFrameCount = 0;
//
//		// Not useful for now
//		// Uncomment to evaluate max fps
//		//if ( m_iFramesPerSecond > m_iMaxFramesPerSecond )
//			//m_iMaxFramesPerSecond = m_iFramesPerSecond;
//	}
//}

void Timer::Reset()
{
	m_iFramesPerSecond = 0;
	m_iFrameCount = 0;
	m_fDelta = 0.0f;
}